﻿using System.Runtime.InteropServices;

namespace Om.Camera.Dualix;

public class SpectrolDllWrapper
{
    // 定义回调委托
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate void VideoRGBFilterDelegate(
        IntPtr pContext,
        IntPtr pRData,
        IntPtr pGData,
        IntPtr pBData,
        uint dataLength
    );

    // 保存委托实例防止GC回收
    private static VideoRGBFilterDelegate _callbackDelegate;

    #region DLL导入声明

    [DllImport("SpectrolDll.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int DualixOpenCamera();

    [DllImport("SpectrolDll.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern bool DualixCloseCamera();

    [DllImport("SpectrolDll.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern bool DualixSetExposure(float expostime);

    [DllImport("SpectrolDll.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern bool DualixGetExposure(ref float expostime);

    [DllImport("SpectrolDll.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern long DualixAddRGBCallback(
        IntPtr videoRGBFilter,
        IntPtr pCBContext
    );

    [DllImport("SpectrolDll.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern int DualixRemoveRGBCallback(long callbackId);

    [DllImport("SpectrolDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    [return: MarshalAs(UnmanagedType.I1)]
    public static extern bool DualixStartSingleCapture(
        [MarshalAs(UnmanagedType.I1)] bool bMotor,
        [MarshalAs(UnmanagedType.I1)] bool bSave,
        [MarshalAs(UnmanagedType.LPStr)] string savePath,
        [MarshalAs(UnmanagedType.I1)] bool bFlip,
        [MarshalAs(UnmanagedType.I1)] bool bRef,
        [MarshalAs(UnmanagedType.I1)] bool bcorrect
    );

    [DllImport("SpectrolDll.dll", CallingConvention = CallingConvention.StdCall)]
    [return: MarshalAs(UnmanagedType.I1)]
    public static extern bool DualixStopSingleCapture();

    #endregion

    #region 回调处理

    // 添加带回调的方法
    public static long AddRGBCallback(VideoRGBFilterDelegate callback, IntPtr context)
    {
        _callbackDelegate = callback;
        IntPtr functionPtr = Marshal.GetFunctionPointerForDelegate(_callbackDelegate);
        return DualixAddRGBCallback(functionPtr, context);
    }

    #endregion

    #region 可选参数封装

    public static bool DualixStartSingleCaptureDefault()
    {
        return DualixStartSingleCapture(
            bMotor: false,
            bSave: false,
            savePath: "",
            bFlip: false,
            bRef: false,
            bcorrect: false
        );
    }

    // 可根据需要添加其他重载版本
    public static bool DualixStartSingleCapture(bool bMotor, bool bSave, string savePath)
    {
        return DualixStartSingleCapture(
            bMotor,
            bSave,
            savePath,
            false,
            false,
            false
        );
    }

    #endregion
}